﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace User_Login_CS
{
    public class DbHelper
    {
        private IDbConnection oConn = null;

        /// <summary>
        /// Returns a connection to the database. If the connection does not exist then it creates one using the connection string mentioned in the webconfig file. 
        /// </summary>
        /// <returns>Object of IDBConnection</returns>
        public bool IsConnected()
        {
            bool blnReturn = false;
            try
            {
                if (oConn == null)
                {
                    // Connect to Database 
                    if (string.Equals(ConfigurationManager.ConnectionStrings["constr"].ProviderName, "mysql", StringComparison.InvariantCultureIgnoreCase))
                    {
                        oConn = new MySqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);                                               
                    }
                    else if (string.Equals(ConfigurationManager.ConnectionStrings["constr"].ProviderName, "mssql", StringComparison.InvariantCultureIgnoreCase))
                    {
                        oConn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
                    }

                    oConn.Open();
                    blnReturn = true;
                }
                else if (oConn.State == ConnectionState.Open)
                {
                    blnReturn = true;
                }
                else
                {
                    oConn.Open();
                    blnReturn = true;
                }
            }
            catch (Exception ex)
            {
                //Logger.log.Error("Error in IsConnected : " + ex.Message);
                
            }
            return blnReturn;
        }

        public int AuthenticateUser(string strUserName, string strPwd)
        {
            int intReturn = -1;
            try
            {
                if (IsConnected())
                {
                    string strCmd = "SELECT UserId FROM users WHERE UserName='" + strUserName + "' AND pwd='" + strPwd + "'";
                    IDbCommand oCmd = oConn.CreateCommand();
                    oCmd.CommandText = strCmd;
                    intReturn = int.Parse(oCmd.ExecuteScalar().ToString());
                    
                }
            }
            catch (Exception ex)
            {
                Logger.log.Error("Error in AuthenticateUser : " + ex.Message);
            }
            finally
            {
                if ( oConn != null)
                {
                    oConn.Close();
                }
            }
            return intReturn;
        }
    }
}